Day 17 - Regular expressions - Groups
92
66.249.73.135 [18/May/2015:15:05:42 GET /misc/Title.php.txt HTTP/1.1 500 - -
Find them and print the IP addresses of each client in a single comma-separated line (i.e. <IP number
1>,<IP number 2>,<IP number 3>)
Solution
The exercise can be solved using grep, sed and xargs‘
$ grep -E "HTTP/1.1 500" simple.log | sed -r s,"([0-9.]+) .*","\1", | xargs echo | s\
ed s/" "/","/g
66.249.73.135,66.249.73.135,64.131.102.243
The initial grep uses the HTTP\1.1 500 mentioned in the text of the exercise to find the relevant lines.
Then, using sed we can extract the IP addresses, put them on a single line with xargs and replacing
the space used as a separator with a comma.
Instead of sed we could have used grep
$ grep -E "HTTP/1.1 500" simple.log | grep -Eo
"^[0-9.]+" | xargs echo | sed s/" "/\
","/g
66.249.73.135,66.249.73.135,64.131.102.243
counting on the fact that the space following the IP address is not matched by the class. A common
replacement for the xargs | sed pattern is the paste command (that wasn’t introduced in the book)
$ grep -E "HTTP/1.1 500" simple.log | grep -Eo
"^[0-9.]+" | paste -s -d,
66.249.73.135,66.249.73.135,64.131.102.243
Make sure you read the man page of the paste command, it’s pretty simple and useful to have in
your toolkit.
Go back to the exercise